package com.xsm.activitygroup;

import java.util.ArrayList;

import com.sina.weibo.sdk.api.TextObject;
import com.xsm.object.CTextObject;

import android.support.v4.app.Fragment;
import android.R.integer;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class FragmentC extends Fragment{
	private ArrayList<CTextObject> arrayListC;
	private Classification context;
	private CTextObject selectBTextObject;
	private ListView  listview;
	private ArrayList<String> valuesC;
	private ArrayList<CTextObject> selectCList;
	private ArrayAdapter<String> adapter;
	private TextView textView;
	private String textString="";
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragmentc, container,false);
	}
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		context=(Classification)getActivity();
		listview=(ListView)context.findViewById(R.id.listc);
		textView=(TextView)context.findViewById(R.id.textString);
		arrayListC=(ArrayList<CTextObject>)context.getIntent().getExtras().get("arrayListC");
		selectCList=new ArrayList<CTextObject>();
		valuesC=new ArrayList<String>();
		adapter=new ArrayAdapter<String>(context,R.layout.listtext,valuesC);
		listview.setAdapter(adapter);
	    FragmentB fragmentB=(FragmentB)getFragmentManager().findFragmentById(R.id.fragment2);
	    selectBTextObject= fragmentB.getBTextObject();
	    UpdateCList(selectBTextObject);
	    listview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
	      		CTextObject object=selectCList.get(position);
	      		String text=textString+"-"+object.getName();
	      		String idString=object.getTextid();
	      		Intent intent =new Intent();
	      		intent.putExtra("textid",idString);
	      		intent.putExtra("textString",text);
	      		getActivity().setResult(getActivity().RESULT_OK,intent);
				getActivity().finish();
			}
		});
	}
	
	public void UpdateCList(CTextObject object){
		String id=object.getTextid();
		String parentid=object.getParentid();
		valuesC.clear();
		selectCList.clear();
		for(int i=0;i<arrayListC.size();i++){
		CTextObject object2=arrayListC.get(i);
		if(object2.getParentid().equals(id)&&object2.getGparentid().equals(parentid))	{
		valuesC.add(object2.getName());	
		selectCList.add(object2);}
		}
		
		listview.setAdapter(adapter);
	}
	
	public void Settext(String s){
		textString=s;
		textView.setText(textString);
	}
}
